﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BUS;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
namespace ProjectM2Work
{
    public partial class GamePage : PhoneApplicationPage
    {
        ContentManager contentManager;
        GameTimer timer;
        GraphicsDevice graphicsDevice;
        SpriteBatch spriteBatch;
        LoadContent loadContent;
        public List<GameScreen> ListGameScreen;

        public GamePage()
        {
            InitializeComponent();

            // Get the content manager from the application
            contentManager = (Application.Current as App).Content;

            // Create a timer for this page
            timer = new GameTimer();
            timer.UpdateInterval = TimeSpan.FromTicks(333333);
            timer.Update += OnUpdate;
            timer.Draw += OnDraw;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Set the sharing mode of the graphics device to turn on XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);
            TouchPanel.EnabledGestures = GestureType.None;

            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);
            Width = SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Width;
            Height = SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Height;
            graphicsDevice = SharedGraphicsDeviceManager.Current.GraphicsDevice;
            // TODO: use this.content to load your game content here

            // Start the timer
            timer.Start();

            Initilation();

            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // Stop the timer
            timer.Stop();

            // Set the sharing mode of the graphics device to turn off XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);

            base.OnNavigatedFrom(e);
        }

        //Quan ly su kien cua cac game screen
        //Function bat su kien show cua mot gamescreen ~ cac gamescreen con lai phai hide
        void ActiveScreen(int index)
        {
            for (int i = 0; i < ListGameScreen.Count; i++)
                if (i != index)
                    ListGameScreen[i].Hide();
        }

        public void ShowScreen(int index)
        {            
            ListGameScreen[index].Show();
            ActiveScreen(index);            
        }

        /// <summary>
        /// Allows the page to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        private void OnUpdate(object sender, GameTimerEventArgs e)
        {
            // TODO: Add your update logic here
            for (int i = 0; i < ListGameScreen.Count; i++) 
                ListGameScreen[i].OnUpdate(e);
        }

        /// <summary>
        /// Allows the page to draw itself.
        /// </summary>
        private void OnDraw(object sender, GameTimerEventArgs e)
        {
            SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);
            for (int i = 0; i < ListGameScreen.Count; i++) 
                ListGameScreen[i].OnDraw(e);
            // TODO: Add your drawing code here
        }

        private void Initilation()
        {
            loadContent = new LoadContent(contentManager);
            TouchPanel.EnabledGestures = GestureType.Pinch | GestureType.Tap |
                   GestureType.FreeDrag | GestureType.Flick | GestureType.DoubleTap |
                   GestureType.DragComplete | GestureType.PinchComplete | GestureType.Hold;
            //Init Gamescreen
            ListGameScreen = new List<GameScreen>();
                // 00 Tao MenuScreen
                GameMenuScreen gameMenuScreen = new GameMenuScreen(this,spriteBatch,contentManager.Load<Texture2D>("XNA\\Background\\bg_game"),loadContent._listMainButton);
                ListGameScreen.Add(gameMenuScreen);
                // 01 Tao TrucXanhScreen
                TrucXanhScreen trucXanhScreen = new TrucXanhScreen(this, spriteBatch, contentManager.Load<Texture2D>("XNA\\Background\\bg_trucxanh"), loadContent._listMainButton, loadContent._listTrucXanh,loadContent._miniMenuFont);
                ListGameScreen.Add(trucXanhScreen);

                // MenuScreen start
                ShowScreen(0);
        }


        public void ShowBack()
        {
            if (NavigationService.CanGoBack)
            {
                NavigationService.GoBack();
            }
        }
        public void GoToTracNghiem()
        {
            NavigationService.Navigate(new Uri("/Pages/PageTracNghiem.xaml", UriKind.Relative));
        }

        public void GoToThongTin()
        {
            NavigationService.Navigate(new Uri("/Pages/PageThongTin.xaml", UriKind.Relative));
        }
    }
}